package cn.net.isir.common;

import cn.net.isir.utils.StringUtils;
import org.apache.log4j.Logger;

import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author liuxubo
 * @filename WxMinipLogFilter.java
 * @package cn.net.isir.common
 * @email lxb@isir.net.cn
 * @date 2018/09/08
 * @comment 小程序请求和返回的过滤器
 * @since 1.7
 */
public class WxMinipLogFilter implements Filter {

    private static Logger logger = Logger.getLogger(WxMinipLogFilter.class);
    private static SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");

    @Override
    public void init(FilterConfig filterConfig) {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain)
            throws IOException, ServletException {
        filterChain.doFilter(servletRequest,servletResponse);
        if (true) {
            return;
        }
        String uuid = StringUtils.randomByTime2();//
        //打印body
        logger.info("[" + uuid + "] request at [" + simpleDateFormat.format(new Date()) + "]");
        logger.info("URL:" + ((HttpServletRequest) servletRequest).getRequestURL());
        logger.info("URI:" + ((HttpServletRequest) servletRequest).getRequestURI());
        logger.info("Method:" + ((HttpServletRequest) servletRequest).getMethod());
        logger.info("QueryString:" + ((HttpServletRequest) servletRequest).getQueryString());
        logger.info("RemoteUser:" + ((HttpServletRequest) servletRequest).getRemoteUser());
        logger.info("RemoteHost:" + servletRequest.getRemoteHost());
        logger.info("RemotePort:" + servletRequest.getRemotePort());
        logger.info("RemoteAddr:" + servletRequest.getRemoteAddr());

        logger.info("getContextPath:" + ((HttpServletRequest) servletRequest).getContextPath());
        String contextPath = ((HttpServletRequest) servletRequest).getContextPath();

        String requestURI = String.valueOf(((HttpServletRequest) servletRequest).getRequestURI());

        //文件下载不打印
        if (requestURI.startsWith(contextPath + "/minip/media/download")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //文件上传不打印
        if (requestURI.startsWith(contextPath + "/minip/media/upload")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }
        //支付通知不打印
        if (requestURI.startsWith(contextPath + "/minip/wxpay/notify")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //将body放入一个新的request实现类，以供后续读取(因为request的输入流只能读取一次，而且无法reset)
//        CustomHttpServletRequest request = new CustomHttpServletRequest(servletRequest);

        logger.info("Content-Type:" + servletRequest.getContentType());
//        request.getCopy();
//        if (servletRequest.getCharacterEncoding() != null) {
//            logger.info("ContentBody:" + new String(request.getCopy(), request.getCharacterEncoding()));
//        } else {
//            logger.info("ContentBody:" + new String(request.getCopy()));
//        }

        //拦截并打印response
        CustomHttpServletResponse response = new CustomHttpServletResponse((HttpServletResponse) servletResponse);
        //使用替换后的request，继续进行后续处理
        filterChain.doFilter(servletRequest, response);
        if (servletResponse.getCharacterEncoding() != null) {
            logger.info("ContentBody:" + new String(response.getCopy(), servletResponse.getCharacterEncoding()));
        } else {
            logger.info("ContentBody:" + new String(response.getCopy()));
        }
        //打印response
        logger.info("[" + uuid + "] response content at [" + simpleDateFormat.format(new Date()) + "]");
    }

    @Override
    public void destroy() {

    }
}
